/*
 * @(#)ConfigADLText.java   09/12/29
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.config;

import fr.ema.arch3d.editor.mainGUI.BNFText.BNFText;
import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.xoadl.configuration.CompositeComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.ConnectorClassADL;
import fr.ema.arch3d.xoadl.configuration.PrimitiveComponentClassADL;

import javax.swing.text.Document;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ConfigADLText {

    /**
     * Method description
     *
     * @param config
     * @return
     */
    public Document createTextConfig(Configuration config) {
        BNFText doc = new BNFText();

        // describe configuration name
        doc.insertBold("configuration");
        doc.insertSpace(1);
        doc.insert(config.getName());
        doc.changeLine();
        doc.insertSpace(1);
        doc.insertBold("implements");

        // describe specification which this configuration implements
        doc.insertSpace(1);
        doc.insert(config.getImplSpecification().getName());
        doc.changeLine();

        // describe components
        doc.insertSpace(1);
        doc.insertBold("component_classes");
        doc.changeLine();

        for (int i = 0; i < config.getComponentImplementation().size(); i++) {
            doc.insertSpace(1);
            doc.insertSpace(1);
            doc.insert(config.getComponentImplementation().get(i).getComponentClassName());
            doc.insertSpace(1);
            doc.insertBold("as");
            doc.insertSpace(1);
            doc.insert(config.getComponentImplementation().get(i).getImplComponentRoleName() + ";");

            // doc.insertBold("implements");
            // doc.insertSpace(1);
            // doc.insert(a[1]+";");
            doc.changeLine();
        }

        // describe connectors
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("connector_classes");
        doc.changeLine();

        for (int i = 0; i < config.getConnectorImplementation().size(); i++) {
            doc.insertSpace(1);
            doc.insertSpace(1);
            doc.insert(config.getConnectorImplementation().get(i).getConnectorClassName());
            doc.insertSpace(1);
            doc.insertBold("as");
            doc.insertSpace(1);
            doc.insert(config.getConnectorImplementation().get(i).getImplConnectionName() + ";");
            doc.changeLine();
        }

        // end describe configuration
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(config.getName());
        doc.changeLine();
        return doc;
    }

    /**
     * Method description
     *
     * @param cmp
     * @return
     */
    public Document createTextPrimitiveCmp(PrimitiveComponentClassADL cmp) {
        BNFText doc = new BNFText();

        // describe component ytpe
        doc.insertBold("component");
        doc.insertSpace(1);
        doc.insert(cmp.getName());
        doc.changeLine();

//
//      // describe implement component type
//      doc.insertSpace(1);
//      doc.insertSpace(1);
//      doc.insertBold("implement");
//      doc.insertSpace(1);
//      doc.insert(cmp.);
//      doc.changeLine();

        // describe component implementation class
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("with_class");
        doc.insertSpace(1);
        doc.insert(cmp.getContent());
        doc.changeLine();

        // describe version information
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("version");
        doc.insertSpace(1);
        doc.insert(cmp.getVersion());
        doc.changeLine();

        // end description
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(cmp.getName());

        return doc;
    }

    /**
     * Method description
     *
     * @param cmp
     * @return
     */
    public Document createTextCompositeCmp(CompositeComponentClassADL cmp) {
        BNFText doc = new BNFText();

        // describe component ytpe
        doc.insertBold("component");
        doc.insertSpace(1);
        doc.insert(cmp.getName());
        doc.changeLine();

//
//      // describe implement component type
//      doc.insertSpace(1);
//      doc.insertSpace(1);
//      doc.insertBold("implement");
//      doc.insertSpace(1);
//      doc.insert(cmp.);
//      doc.changeLine();

        // describe component implementation class
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("with_class");
        doc.insertSpace(1);
        doc.insert(cmp.getContent().getName() + " " + cmp.getContent().getVersion());
        doc.changeLine();

        // describe version information
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("version");
        doc.insertSpace(1);
        doc.insert(cmp.getVersion());
        doc.changeLine();

        // end description
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(cmp.getName());

        return doc;
    }

    /**
     *
     * @param ctr
     * @return the document which describe the connection using ADL grammar
     */
    public Document createTextCnt(ConnectorClassADL ctr) {
        BNFText doc = new BNFText();

        // describe connector
        doc.insertBold("connector");
        doc.insertSpace(1);
        doc.insert(ctr.getName());
        doc.changeLine();

//      // describe implement connection
//      doc.insertSpace(1);
//      doc.insertSpace(1);
//      doc.insertBold("implement");
//      doc.insertSpace(1);
//      doc.insert(ctr.connection.name);
//      doc.changeLine();
        // describe connector implementation class
        doc.insertSpace(1);
        doc.insertSpace(1);
        doc.insertBold("with_class");
        doc.insertSpace(1);
        doc.insert(ctr.getContent());
        doc.changeLine();

        // end description
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(ctr.getName());

        return doc;
    }
}
